import { SpriteFrame, Vec2, v2 } from 'cc'
import { UserInfo } from '../common/UserInfo'
import WorldManager from '../common/WorldManager'
import { villageType } from '../settings/const'
import ResManager from '../common/ResManager'
export class MapZone {
    aId: number
    allyName: string
    field: number
    openid: string
    owner: string
    people: number
    race:number
    typeId: number
    uid: number
    x: number
    y: number
    zoneId: number
    zoneName: string
    faithful:number
    villageName:string
    st:number
    eventHandle:number
    regTime:number

    /*
    o_food50: 1, o_wood50: 2, o_iron50: 3, o_mud50: 4,
    o_food25mud25: 5, o_food25iron25: 6, o_food25wood25: 7,
    o_wood25: 8, o_iron25: 9, o_mud25: 10, o_food25: 11,
    v_f18: 21, v_f15i1m1w1: 22, v_f9i3m3w3: 23,
    v_f6i4m3w5: 24, v_f6i5m3w4: 25, v_f6i6m3w3: 26,
    v_f6i3m4w5: 27, v_f6i4m4w4: 28, v_f6i5m4w3: 29,
    v_f6i3m5w4: 30, v_f6i4m5w3: 31, v_f6i3m6w3: 32, v_f6i3m3w6: 33
    **/

    /**
     * 根据数据创建
     * @param obj 
     * @returns 
     */
    public static createByObj(obj):MapZone{
        let zone = new MapZone()
        for(let key in obj){
            zone[key] = obj[key]
        }

        let xy = MapZone.getXyByZoneId(zone.zoneId)
        zone.x = xy[0]
        zone.y = xy[1]
        return zone
    }

    /**
     * 资源田的类型id 转成具体资源数量
     * @param typeId 资源田的类型id
     * @returns 
     */
    public static zoneTypeToNum = (typeId: number) => {
        let name = ''
        for (let key in villageType) {

            if (villageType[key] == typeId) {
                name = key
            }
        }

        name = name.replace('o_', '')
        name = name.replace('v_', '')
        name = name.replace('food', 'f')
        name = name.replace('wood', 'w')
        name = name.replace('iron', 'i')
        name = name.replace('mud', 'm')

        let k = name.match(/[a-z]/g)
        let v = name.match(/\d+/g)
        let obj = {}
        for (let i = 0; i < k.length; i++) {
            obj[k[i]] = parseInt(v[i])
        }
        return obj
    }


    /**
     * zoneId 转成 X,Y
     * @param zoneId 
     * @param radius 地图的半径，默认400
     * @returns 
     */
    static getXyByZoneId(zoneId: number, radius: number = 400): number[] {
        let x = ((zoneId / (radius * 2 + 1)) | 0) - radius;
        let y = (zoneId % (radius * 2 + 1)) - (radius + 1);
        if (y == -(radius + 1)) {
            y = radius;
            x--;
        }

        if (x > radius || x < - radius) {
            x = (x > radius) ? (x - (radius * 2 + 1)) : (x + (radius * 2 + 1));
        }
        if (y > radius || y < - radius) {
            y = (y > radius) ? (y - (radius * 2 + 1)) : (y + (radius * 2 + 1));
        }
        return [x, y];
    }

    /**
     * 将坐标值转化为 zoneId
     * @param {Number} x 
     * @param {Number} y 
     * @param {Number} square 地图的半径，默认400
     * @returns {Number} zoneId
     */
    static getZoneIdByXy(x: number, y: number, square: number = 400): number {
        if (x > square || x < -square) {
            x = (x > square) ? (x - (square * 2 + 1)) : (x + (square * 2 + 1));
        }
        if (y > square || y < -square) {
            y = (y > square) ? (y - (square * 2 + 1)) : (y + (square * 2 + 1));
        }
        return (((x + square) * (square * 2 + 1)) + (y + (square + 1)));
    }

    /**
    * 计算任意两点之间的相对距离
    * @param {Number} A
    * @param {Number} B
    * @returns {Number} A点和B点之间的距离
    */
    static distance(zidA: number, zidB: number): number {
        let square = WorldManager.instance.radius
      
        let point = MapZone.getXyByZoneId(zidA, square)
        let zA = v2(point[0], point[1])
        point = MapZone.getXyByZoneId(zidB, square)
        let zB = v2(point[0], point[1])
        let distance = Vec2.distance(v2(zA), v2(zB))
        return distance / UserInfo.instance.speed
    }

    /**
     * 获取绿洲的图片
     * @returns 
     */
    async getImg():Promise<SpriteFrame>{
        let sf = await ResManager.instance.loadAssetByUrl<SpriteFrame>('zoneType/' + this.typeId + '/spriteFrame')
        return sf
    }

    /**
     * 获取绿洲的资源比例
     */
    get desc():string{
        let obj = MapZone.zoneTypeToNum(this.typeId)
        let list = []
        for(let key in obj){

            switch(key){
                case 'f':
                    list.push(`粮食+${obj[key]}%`)
                break
                case 'w':
                    list.push(`木材+${obj[key]}%`)
                break
                case 'i':
                    list.push(`铁矿+${obj[key]}%`)
                break
                case 'm':
                    list.push(`石矿+${obj[key]}%`)
                break
            }
        }
        return list.join()
    }

    /**
     * 判断注册时间是否超过3天
     */
    get isOverThreeDay():boolean{
        let now = new Date().getTime() / 1000
        let day = (now - this.regTime) / 3600 / 24
        return day > 3
    }
}


